﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mani.Patient.Business.Dao;
using Mani.Patient.Business.Domain.Entity;
using Spring.Transaction.Interceptor;

namespace Mani.Patient.Business.Services {

    public interface IPatientService {

        IList<Mani.Patient.Business.Domain.Entity.Patient> FindPatients(int pageSize, int pageNumber, string sort, bool asc, string patientName, string fileNumber);
        int CountFindPatients(string patientName, string fileNumber);
        Patient.Business.Domain.Entity.Patient SavePatient( Patient.Business.Domain.Entity.Patient patient);
        Patient.Business.Domain.Entity.Patient GetPatientById(int id);
        void DeletePatient(int id);
        void SubmitVisit(Visit visit);
        IList<Visit> GetVisitHistory(Patient.Business.Domain.Entity.Patient p, int pageSize, int pageNumber);
        int GetCountVisitHistory(Patient.Business.Domain.Entity.Patient p);
        Visit GetVisitHistoryById(int id);
        void UpdateVisitHistory(Visit v);
        void DeleteVisitHistory(int id);
        string SuggestFileNumber();
    }
    
    public class PatientService : IPatientService {

        public IPatientDao PatientDao { get; set; }
        public IFileDao FileDao { get; set; }
        public VisitDao VisitDao { get; set; }

        public int CountFindPatients(string patientName, string fileNumber) {
            File file = null;

            if (!String.IsNullOrEmpty(fileNumber))
                file = FileDao.FindByFileNumber(fileNumber);

            return PatientDao.CountFindPatients(patientName, file);
        }

        public IList<Mani.Patient.Business.Domain.Entity.Patient> FindPatients(int pageSize, int pageNumber, string sort, bool asc, string patientName, string fileNumber) {

            File file = null;

            if (!String.IsNullOrEmpty(fileNumber))
                file = FileDao.FindByFileNumber(fileNumber);
                

            return PatientDao.FindPatients(pageSize, pageNumber, sort, asc, patientName, file);
        }

        [Transaction(ReadOnly=false)]
        public Patient.Business.Domain.Entity.Patient SavePatient(Patient.Business.Domain.Entity.Patient patient) {
            return PatientDao.SaveOrUpdate(patient);
        }

        public Patient.Business.Domain.Entity.Patient GetPatientById(int id) {
            return PatientDao.FindById(id);
        }

        [Transaction(ReadOnly = false)]
        public void DeletePatient(int id) {
            PatientDao.Delete(PatientDao.FindById(id));
        }


        [Transaction(ReadOnly = false)]
        public void SubmitVisit(Visit visit) {
            VisitDao.SaveOrUpdate(visit);
        }

        public IList<Visit> GetVisitHistory(Patient.Business.Domain.Entity.Patient p, int pageSize, int pageNumber)
        {
            return VisitDao.FindByPatient(pageSize, pageNumber, p);
        }

        public int GetCountVisitHistory(Patient.Business.Domain.Entity.Patient p)
        {
            return VisitDao.GetCountVisit(p);
        }

        public Visit GetVisitHistoryById(int id)
        {
            return VisitDao.FindById(id);
        }

        [Transaction(ReadOnly = false)]
        public void UpdateVisitHistory(Visit v)
        {
            VisitDao.SaveOrUpdate(v);
        }

        [Transaction(ReadOnly = false)]
        public void DeleteVisitHistory(int id)
        {
            VisitDao.Delete(VisitDao.FindById(id));
        }

        public string SuggestFileNumber() {
            string result = "";

            result += Mani.Common.MyPersianDate.Persian.GetYear(DateTime.Now).ToString().Substring(2,2);
            result += Mani.Common.MyPersianDate.Persian.GetMonth(DateTime.Now).ToString("00");

            return FileDao.GetNextFileNumber(result); ;
        }
    }
}
